package com.servicelive.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import antlr.StringUtils;

import com.ctc.wstx.util.StringUtil;
import com.wavemaker.runtime.javaservice.JavaServiceSuperClass;

public class SMSService extends JavaServiceSuperClass {
	/*
	 * Pass in one of FATAL, ERROR, WARN, INFO and DEBUG to modify your log
	 * level; recommend changing this to FATAL or ERROR before deploying. For
	 * info on these levels, look for tomcat/log4j documentation
	 */

	public SMSService() {
		super(INFO);
	}

	public static String retval = "";

	/*
	 * private String user = ""; private String password = ""; private String
	 * sid="";
	 * 
	 * public String getUser() { return user; }
	 * 
	 * public void setUser(String user) { this.user = user; }
	 * 
	 * public String getPassword() { return password; }
	 * 
	 * public void setPassword(String password) { this.password = password; }
	 * 
	 * public String getSid() { return sid; }
	 * 
	 * public void setSid(String sid) { this.sid = sid; }
	 */
	public String SMSSender(String msisdn, String msg) {
		String rsp = "";
		String user = "TPSH2M";
		String password = "829484";
		String sid = "TPSHMI";
		String fl = "0";

		try {
			// Construct The Post Data
			String data = URLEncoder.encode("user", "UTF-8") + "="
					+ URLEncoder.encode(user, "UTF-8");
			data += "&" + URLEncoder.encode("password", "UTF-8") + "="
					+ URLEncoder.encode(password, "UTF-8");
			data += "&" + URLEncoder.encode("msisdn", "UTF-8") + "="
					+ URLEncoder.encode(msisdn, "UTF-8");
			data += "&" + URLEncoder.encode("msg", "UTF-8") + "="
					+ URLEncoder.encode(msg, "UTF-8");
			data += "&" + URLEncoder.encode("sid", "UTF-8") + "="
					+ URLEncoder.encode(sid, "UTF-8");
			data += "&" + URLEncoder.encode("fl", "UTF-8") + "="
					+ URLEncoder.encode(fl, "UTF-8");
			data += "&" + URLEncoder.encode("gwid", "UTF-8") + "="
					+ URLEncoder.encode("2", "UTF-8");

			// Push the HTTP Request
			URL url = new URL("http://smslane.com/vendorsms/pushsms.aspx");
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);

			OutputStreamWriter wr = new OutputStreamWriter(
					conn.getOutputStream());
			wr.write(data);
			wr.flush();

			// Read The Response
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String line;
			while ((line = rd.readLine()) != null) {
				// Process line...
				retval += line;
			}
			wr.close();
			rd.close();

			System.out.println(retval);
			rsp = retval;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return rsp;
	}

	public String ConstructTemplate(String vendorName, String customerName,
			String address, String phoneNumber, String orderId, String blockId,
			String doorId, String orderDetail) {
		String SmsTemplate = "Name : #Name# , Order No: #Order# Address : #Address# ,Block #block# ,Door #door# , Ph: #phone# , Item Details : #items# . Thanks, H2M.";
		String sMessage = "";
		sMessage = SmsTemplate.replaceFirst("#Name#", customerName);
		sMessage = sMessage.replaceFirst("#Order#", orderId);
		if (address != null && address != "") {
			sMessage = sMessage.replaceFirst("#Address#", address);
		} else {
			sMessage = sMessage.replaceFirst("#Address#", "No Address");
		}
		sMessage = sMessage.replaceFirst("#block#", blockId);
		sMessage = sMessage.replaceFirst("#door#", doorId);
		sMessage = sMessage.replaceFirst("#phone#", phoneNumber);
		sMessage = sMessage.replaceFirst("#items#", orderDetail);

		System.out.println("sMessage @@@" + sMessage);
		return sMessage;
	}

}
